﻿using Microsoft.Extensions.FileProviders;
using Newtonsoft.Json;
using System.Text;

namespace LogTestApp
{
    public class JsonFileLoad
    {
        public List<HangfireOptions> GetCaseCodes()
        {
            return this.GetDataByLocal<List<HangfireOptions>>("GetHangfireOptions.json");
        }

        private T GetDataByLocal<T>(string jsonfileName)
        {
            var filePath = $"/UploadFile/";
          
            string applicationRoot = AppContext.BaseDirectory;
            var provider = new PhysicalFileProvider(applicationRoot);
            var completeFilePath = Path.Combine(filePath, jsonfileName);
            if (!System.IO.File.Exists(AppContext.BaseDirectory + completeFilePath))
            {
                throw new Exception("文件不存在");
            }
            var formFile = provider.GetFileInfo(completeFilePath);
            var stream = formFile.CreateReadStream();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
            Encoding encoding = Encoding.GetEncoding("gb2312");
            using var reader = new StreamReader(stream, encoding);
            string rs = reader.ReadToEnd();
            var result = JsonConvert.DeserializeObject<T>(rs);
            return result;
        }
    }

    public class HangfireOptions
    {
        public int WorkerCount { get; set; }
        public string StorageConnectionString { get; set; }
        public string DashboardUrl { get; set; }
    }
}